package com.driving.code.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.driving.code.dao.CollectionMapper;
import com.driving.code.dao.TTypeMapper;
import com.driving.code.dto.CollectionDto;
import com.driving.code.dto.TypeDto;
import com.driving.code.entity.Collection;
import com.driving.code.entity.TType;
import com.driving.code.service.CollectionService;
import com.driving.code.utils.CopyUtils;
import com.driving.code.vo.CollectionVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CollectionServiceImpl implements CollectionService {

    @Resource
    private CollectionMapper collectionMapper;
    @Resource
    private TTypeMapper typeMapper;

    @Override
    public List<TypeDto> selectTypeByUserId(Integer userId) {
        List<Collection> collectionList = collectionMapper.selectList(new QueryWrapper<Collection>()
                .lambda()
                .eq(Collection::getUserId, userId)
                .select(Collection::getCollectionId, Collection::getUserId, Collection::getTypeId));
        //获取typeId
        Set<Integer> typeSet = collectionList.stream().map(Collection::getTypeId).collect(Collectors.toSet());

        List<TypeDto> dtoList = null;
        if (typeSet.size()>0){
             List<TType> typeList = typeMapper.selectList(new QueryWrapper<TType>()
                    .lambda()
                    .in(TType::getTypeId,typeSet)
                    .select(TType::getTypeName, TType::getTypeId));
            dtoList = CopyUtils.copyList(typeList,TypeDto.class);
            return dtoList;
        }else {
            return dtoList;
        }

    }

    @Override
    public List<CollectionDto> selectQuestionByUserId(Integer userId) {
        List<Collection> collectionList = collectionMapper.selectList(new QueryWrapper<Collection>()
                .lambda()
                .eq(Collection::getUserId, userId)
                .select(Collection::getCollectionId, Collection::getUserId, Collection::getTypeId,Collection::getQuestionId,Collection::getCreateTime));
        List<CollectionDto> dtoList = CopyUtils.copyList(collectionList,CollectionDto.class);
        return dtoList;
    }

    @Override
    public List<CollectionDto> selectQuestionByUserIdAndType(Integer userId, Integer typeId) {
        List<Collection> collectionList = collectionMapper.selectList(new QueryWrapper<Collection>()
                .lambda()
                .eq(Collection::getUserId, userId)
                .eq(Collection::getTypeId,typeId)
                .select(Collection::getCollectionId, Collection::getUserId, Collection::getTypeId,Collection::getQuestionId,Collection::getCreateTime));
        List<CollectionDto> dtoList = CopyUtils.copyList(collectionList,CollectionDto.class);
        return dtoList;
    }

    @Override
    public int isCollection(Integer userId, Integer question) {
        List<Collection> collections = collectionMapper.selectList(new QueryWrapper<Collection>()
                .lambda()
                .eq(Collection::getUserId, userId)
                .eq(Collection::getQuestionId, question)
                .select(Collection::getCollectionId));
        if ( !ObjectUtils.isEmpty(collections)){
            return 1;
        }
        return 0;
    }

    @Override
    public int saveCollectionQuestion(CollectionVo collectionVo) {
        Collection collection = new Collection();
        collection.setUserId(collectionVo.getUserId());
        collection.setTypeId(collectionVo.getTypeId());
        collection.setCreateTime(new Date());
        collection.setIsDelete(0);
        int insert = collectionMapper.insert(collection);
        return insert;
    }
}
